package com.woniuxy.joylifeshop.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.ShopCommodity;
import com.woniuxy.common.entity.ShopMerchant;
import com.woniuxy.common.entity.ShopMerchantAccount;
import com.woniuxy.joylifeshop.exception.MerchantException;
import com.woniuxy.joylifeshop.service.CommodityService;
import com.woniuxy.joylifeshop.service.MerchantAccountService;
import com.woniuxy.joylifeshop.service.MerchantService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import com.woniuxy.joylifeshop.vo.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MerchantController {

    @Autowired
    MerchantService merchantService;

    @Autowired
    CommodityService commodityService;

    @Autowired
    MerchantAccountService merchantAccountService;

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @RequestMapping("/initMerchantId")
    public String initMerchantId(String shopId){
        ShopCommodity shopCommodity = commodityService.readCommodityByIdSimple(shopId);
        return shopCommodity.getMerchantId();
    }

    /**
    * @Author SooYoo
    * @Description 商家的金钱管理
    * @Param
    * @Return
    **/
    @RequestMapping("/billManager")
    public ViewObject billManager(@RequestBody HashMap<String,String> information){
        //从json中获取数据
        String jsonstr = information.get("jsonstr");
        ShopMerchant shopMerchant = JSON.parseObject(jsonstr, ShopMerchant.class);
        System.out.println(shopMerchant);
        //封装VO
        ViewObject viewObject = new ViewObject();
        //分页
        PageHelper.startPage(Integer.parseInt(information.get("page")),Integer.parseInt(information.get("limit")));
        //找到需要的收入对象
        List<ShopMerchantAccount> shopMerchantAccounts = merchantAccountService.readMerchantAccountObject(shopMerchant);
        //分页2
        PageInfo pageInfo = new PageInfo(shopMerchantAccounts);
        List<PageInfo> pageInfos = new ArrayList<>();
        pageInfos.add(pageInfo);
        //配置vo
        viewObject.setData(pageInfos);
        viewObject.setCode(0);
        viewObject.setCount(new Long(pageInfo.getTotal()).intValue());
        viewObject.setMsg("查询成功");
        return viewObject ;

    }

    /**
    * @Author SooYoo
    * @Description 通过商店id找到这个商店的所有的商品.
    * @Param
    * @Return
    **/
    @RequestMapping("/initCommodityItem")
    public PageInfo initCommodityItem(String merchantId,String pageNumber,String pageSize){
        //System.out.println(merchantId);

        PageHelper.startPage(Integer.parseInt(pageNumber),Integer.parseInt(pageSize));

        List<ShopCommodity> shopCommodities = commodityService.readCommoditysByMerchantId(merchantId);

        PageInfo pageInfo = new PageInfo(shopCommodities);

        return pageInfo;
    }



    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @RequestMapping("/findMerchantByMerchantId")
    public ResponseInformation findMerchantByMerchantId(String merchantId){
        //System.out.println(merchantId);
        ResponseInformation responseInformation = new ResponseInformation();
        try {
            ShopMerchant shopMerchant = merchantService.readMerchantByMerchantId(merchantId);

            responseInformation.setState(200);
            responseInformation.setMsg("success");
            responseInformation.setData(shopMerchant);

            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();

            responseInformation.setMsg("fail");
            responseInformation.setState(500);

            return responseInformation;
        }

    }


    /**
    * @Author SooYoo
    * @Description 跳转到商家页面
    * @Param
    * @Return
    **/
    @RequestMapping("/goMerchant")
    public ResponseInformation goMerchant (String shopId){

        ResponseInformation responseInformation = new ResponseInformation();
        try {
            //用shopId找到商家对象
            ShopMerchant shopMerchant = merchantService.readMerchantByShopId(shopId);

            responseInformation.setState(200);
            responseInformation.setMsg("success");
            responseInformation.setData(shopMerchant);

            return responseInformation;
        } catch (MerchantException e) {
            e.printStackTrace();

            responseInformation.setMsg("fail");
            responseInformation.setState(500);

            return responseInformation;
        }
    }




}
